

# 传入的数据:分数
# 写入到最后一行
import pygame
from pygame.rect import Rect


def write_data(score):
    f = open('rankList.txt', 'r+')
    f.read()
    f.write('\n' + str(score))
    f.close()


def sort_scores():
    f = open('rankList.txt', 'r+')
    scores = []
    for score in f.readlines():
        score = score.replace("\n", "").strip()
        if score == '':
            score = 0
        score = int(score)
        scores.append(score)

    f.close()

    scores.sort()
    scores.reverse()

    return scores


def draw_rank_list(screen, score):
    # -->记录分数

    scores = sort_scores()

    # 设置分数
    rect1 = Rect(180, 140, 600, 210)
    pygame.draw.rect(screen, (255, 255, 255), rect1)
    font = pygame.font.SysFont('SimHei', 20)
    font1 = pygame.font.SysFont('SimHei', 25)

    from main import print_text
    print_text(screen, font1, 440, 150, '排行榜')
    print_text(
        screen,
        font1,
        220,
        175,
        '----------------------------------------')
    count = 1
    for s in scores:
        if count <= 5:
            # 设置按钮文字大小，字体，位置
            from main import print_text
            print_text(
                screen,
                font,
                280,
                180 +
                25 *
                count,
                str(count) +
                '.  ' +
                str(s))
            count = count + 1
        elif 10 >= count > 5:
            from main import print_text
            print_text(screen, font, 580, 180 + 25 *
                       (count - 5), str(count) + '.  ' + str(s))
            count = count + 1
        else:
            break
